var ns, FNScanner;
var app = new Vue({
    el: '#app',
    data: {
        form: {
            lightIsOn: false,
            id: 0,
            isclose: false,
            type: 0, //2-巡检维保扫码 3-巡更 5-保洁 8-二维码登录 9-访客通行 10-任务巡检 11-安防巡更 13-物品放行 14-服务卡扫码收款
            EncriptID: '',
            taskid: 0,
        }
    },
    methods: {
        openActionsheet: function() {
            var that = this;
            ns.confirmPer('camera', function() {
                setTimeout(function() {
                    that.openScanView();
                }, 200)
            })
        },
        openScanView: function() {
            var that = this;
            //判断是否隐藏相册按钮
            var isAlbum=false;
            if(that.form.type==10||that.form.type==3||that.form.type==2||that.form.type==5){
                isAlbum=true;
            }
            FNScanner.open({
                sound: 'widget://res/ding.wav',
                autorotation: true,
                isAlbum:isAlbum,
                hintText: '对准二维码, 即可自动扫描'
            }, function (ret, err) {
                // console.log(JSON.stringify(ret));
                if (ret) {
                    if (ret.eventType == 'cancel') {
                        that.closeWin();
                        return;
                    }
                    if (ret.eventType == 'cameraError') {
                        api.alert({
                            title: '提示',
                            msg: '访问摄像头失败',
                        }, function() {
                            that.closeWin();
                        });
                        return;
                    }
                    if (ret.eventType == 'albumError') {
                        api.alert({
                            title: '提示',
                            msg: '访问相册失败',
                        }, function() {
                            that.closeWin();
                        });
                        return;
                    }
                    if (ret.eventType == 'fail') {
                        api.alert({
                            title: '提示',
                            msg: '扫码失败',
                        }, function() {
                            that.closeWin();
                        });
                        return;
                    }
                    if (ret.eventType == "cancelImage") {
                        that.closeWin();
                        return;
                    }
                    if (ret.eventType == "success") {
                        that.form.id = ret.content;
                        // alert(JSON.stringify(ret.content));
                        if (that.form.type <= 0) {
                            that.do_check_scan(ret);
                        } else {
                            that.do_proces_scan(ret);
                        }
                    }
                }
            });
        },
        do_proces_scan: function (ret) {
            var that = this;
            // alert(JSON.stringify(that.form.EncriptID));
           if (that.form.type == 9) {
                ns.openWin('scanner_result_frm', '访客通行记录', {
                    id: ret.content
                });
            } else if (that.form.type == 8) {
                ns.openWin('qrcodelogin_result_frm', '二维码登录', {
                    id: ret.content
                });
            } else if (that.form.type == 2) {
                if (that.form.EncriptID == '') {
                    ns.openWin('equipment_addfrm', '新增记录', {
                        EncriptID: that.form.id
                    });
                } else {
                    if (that.form.EncriptID != that.form.id) {
                        api.alert({
                            title: '提示',
                            msg: '二维码无效',
                        }, function(ret, err) {
                            that.closeWin();
                        });
                        return;
                    }
                    // alert(JSON.stringify(that.form.taskid));
                    ns.openWin('equipment_details_frm', '巡检设备', {
                        id: that.form.taskid
                    });
                }
            } else if (that.form.type == 10) {
                if (that.form.id == '') {
                    api.alert({
                        title: '提示',
                        msg: '二维码无效',
                    }, function(ret, err) {
                        that.closeWin();
                    });
                    return;
                } else {
                    api.sendEvent({
                        name:'scandeviceqrcodedone',
                        extra: {
                            id: that.form.id
                        }
                    })
                    setTimeout(function(){
                        that.closeWin();
                    },1000)
                }
            }
            // else if (that.form.type == 11) {
            //     if (that.form.id == '') {
            //         api.alert({
            //             title: '提示',
            //             msg: '二维码无效',
            //         }, function(ret, err) {
            //             that.closeWin();
            //         });
            //         return;
            //     } else {
            //         api.sendEvent({
            //             name:'scandeviceqrcodedone',
            //             extra: {
            //                 id: that.form.id
            //             }
            //         })
            //         setTimeout(function(){
            //             that.closeWin();
            //         },1000)
            //     }
            // }
            else if (that.form.type == 3) {
                if (that.form.EncriptID == '') {
                    ns.openWin('patrolplan_addfrm', '新增记录', {
                        EncriptID: that.form.id
                    });
                } else {
                    let IsLine = api.getPrefs({
                        sync: true,
                        key: 'lineStatus'
                    });
                    if (that.form.EncriptID != that.form.id) {
                        api.alert({
                            title: '提示',
                            msg: '二维码无效',
                        }, function(ret, err) {
                            that.closeWin();
                        });
                        return;
                    }
                    if(IsLine==1){
                        ns.openWin('renwulist_off_details', '巡更点', {
                            id: that.form.taskid
                        });
                    }
                    else{
                        ns.openWin('patrolplan_details_frm', '巡更点', {
                            id: that.form.taskid
                        });
                    }
                   
                }
            } else if (that.form.type == 5) {
                if (that.form.EncriptID == '') {
                    ns.openWin('bjlh_addfrm', '新增记录', {
                        EncriptID: that.form.id
                    });
                } else {
                    if (that.form.EncriptID != that.form.id) {
                        api.alert({
                            title: '提示',
                            msg: '二维码无效',
                        }, function(ret, err) {
                            that.closeWin();
                        });
                        return;
                    }
                    ns.openWin('bjlh_details_frm', '保洁区域', {
                        id: that.form.taskid
                    });
                }
            } else if(that.form.type == 13){
                ns.openWin('scanner_release_frm', '放行条', {
                    id: ret.content
                });
            }
            that.closeWin();
        },
        do_check_scan: function(ret) {
            var that = this;
            // console.log('2222222222222');
            ns.post({
                action: 'checkscanresult',
                content: that.form.id
            }, function (succeed, data, err) {
                if (succeed) {
                    //alert(JSON.stringify(data));
                    that.form.type = data.QrCodeType;
                    if(that.form.type==14){
                        that.do_select(ret);
                    }else{
                        that.do_proces_scan(ret);
                    }
                    
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.closeWin();
                }
            });
        },
        do_select:function(ret){
            var that = this;
            ns.post({
                action: 'getscanservicecard',
                ID: ret.content,
            }, function (succeed, data, err) {
                // console.log(JSON.stringify(ret.content));
                if (succeed) {
                    ns.openWin('service_saoma_add', '新增收款', {
                        item: data.data,
                        bg: "#289FFF",
                        txtcolor: "#ffffff",
                    });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.closeWin();
                }
            });
        },
        closeWin: function() {
            var that = this;
            setTimeout(function() {
                api.closeFrame();
            }, 500);
        }
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.form.type = api.pageParam.type || 0;
    app.form.taskid = api.pageParam.id || 0;
    app.form.EncriptID = api.pageParam.EncriptID || ''
    FNScanner = api.require('FNScanner');
    app.openActionsheet();
    api.addEventListener({
        name: 'do_close_scanner'
    }, function(ret, err) {
        app.closeWin();
    });
}
